package com.misday.eatup.model;

import java.util.ArrayList;
import java.util.List;
import com.misday.eatup.R;
import com.misday.eatup.R.drawable;

import android.content.ContentValues;
import android.content.Context;
import android.database.AbstractCursor;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;

public class DigestCursor extends AbstractCursor {
	
	private static final String TAG = "DigestCursor";
	
	private static final String[] COLUMNS = new String[] {
		FoodsColumns._ID,
		FoodsColumns.FOOD,
		FoodsColumns.PRIORITY,
		FoodsColumns.DETAIL,
	};
	private static final int COLUMN_ID = 0;
	private static final int COLUMN_FOOD = 1;
	private static final int COLUMN_PRIORITY = 2;
	private static final int COLUMN_DETAIL = 3;
	
	private FoodsDatabaseHelper mFDBHelper;
	private int mIndex;	
	private List<ContentValues> mDigestList;
	
	public DigestCursor(Context context, ArrayList<String> somethingArray) {
		mIndex = 0;
		mDigestList = new ArrayList<ContentValues>();
		
		mFDBHelper = new FoodsDatabaseHelper(context);
		
		if (somethingArray.size() == 1) {
			checkConflict(somethingArray.get(0));
		} else if (somethingArray.size() > 1) {
			checkConflict(somethingArray);
		} else {}
	}
	
	private void checkUnsuitable(String something) {
		Log.i(TAG, "eat: " + something);
		
		Cursor c = mFDBHelper.query(something);
		Log.d(TAG, "+++get cursor + " + c);
		if (c == null) {
			Log.e(TAG, "cursor is null");
			return;
		}
		
		int count = c.getCount();
		if (count < 1) {
			Log.e(TAG, "count = " + count);
			Log.d(TAG, "---close cursor + " + c);
			c.close();
			c = null;
			return;
		}
		
		c.moveToFirst();
		for (int i = 0; i < count; i++) {
			int idx;
			
			idx = c.getColumnIndex(FoodsColumns.FOOD);
			String food = c.getString(idx);
			idx = c.getColumnIndex(FoodsColumns.PRIORITY);
			String priority = c.getString(idx);
			idx = c.getColumnIndex(FoodsColumns.DETAIL);
			String detail = c.getString(idx);
			/*
			Log.i(TAG, "UNSUITABLE: " + food + ", " + priority + ", " + detail);
			
			ContentValues item = new ContentValues();
			item.put(FoodsColumns.FOOD, food);
			item.put(FoodsColumns.PRIORITY, priority);
			item.put(FoodsColumns.DETAIL, detail);
			mDigestList.add(item);
			*/
			appendItem(food, priority, detail);
			
			c.moveToNext();
		}
		Log.d(TAG, "---close cursor + " + c);
		c.close();
		c = null;
	}
	
	private void checkConflict(String something) {
		Cursor c;
		String food1;
		String food2;
		String priority;
		String detail;
		int idx;
		int count;
		
		Log.i(TAG, "eat: " + something);
		
		c = mFDBHelper.query(something);
		Log.d(TAG, "+++get cursor + " + c);
		if (c == null) {
			Log.e(TAG, "cursor is null");
			return;
		}
		
		count = c.getCount();
		if (count < 1) {
			Log.e(TAG, "count = " + count);
			appendOKItem(something);
			Log.d(TAG, "---close cursor + " + c);
			c.close();
			c = null;
			return;
		}
		
		c.moveToFirst();
		for (int i = 0; i < count; i++) {
			idx = c.getColumnIndex(FoodsColumns.FOOD1);
			food1 = c.getString(idx);
			idx = c.getColumnIndex(FoodsColumns.FOOD2);
			food2 = c.getString(idx);
			idx = c.getColumnIndex(FoodsColumns.PRIORITY);
			priority = c.getString(idx);
			idx = c.getColumnIndex(FoodsColumns.DETAIL);
			detail = c.getString(idx);

			appendItem(food1, food2, priority, detail);
			
			c.moveToNext();
		}
		Log.d(TAG, "---close cursor + " + c);
		c.close();
		c = null;
	}
	
	private void checkConflict(ArrayList<String> list) {
		String food1;
		String food2;
		String priority;
		String detail;
		int idx;
		Cursor c;
		int count;
		
		for (int i = 0; i < list.size(); i++) {
			for (int j = i + 1; j < list.size(); j++) {
				food1 = list.get(i);
				food2 = list.get(j);
				
				Log.i(TAG, "eat: " + food1 + " + " + food2);
				
				c = mFDBHelper.query(food1, food2);
				Log.d(TAG, "+++get cursor + " + c);
				if (c == null) {
					Log.e(TAG, "" + i + " vs " + j + " cursor is null");
					continue;
				}
				
				count = c.getCount();
				if (count < 1) {
					Log.e(TAG, "" + i + " vs " + j + " count = " + count);
					appendOKItem(food1, food2);
					Log.d(TAG, "---close cursor + " + c);
					c.close();
					c = null;
					continue;
				}
				
				c.moveToFirst();
				for (int k = 0; k < count; k++) {
					idx = c.getColumnIndex(FoodsColumns.FOOD1);
					food1 = c.getString(idx);
					idx = c.getColumnIndex(FoodsColumns.FOOD2);
					food2 = c.getString(idx);
					idx = c.getColumnIndex(FoodsColumns.PRIORITY);
					priority = c.getString(idx);
					idx = c.getColumnIndex(FoodsColumns.DETAIL);
					detail = c.getString(idx);
					
					/*
					Log.i(TAG, "CONFLICT: " + food1 + ", " + food2 + ", " + priority + ", " + detail);
					ContentValues item = new ContentValues();
					item.put(FoodsColumns.FOOD, food1 + " + " + food2);
					item.put(FoodsColumns.PRIORITY, priority);
					item.put(FoodsColumns.DETAIL, detail);
					this.mDigestList.add(item);
					*/
					appendItem(food1, food2, priority, detail);
					
					c.moveToNext();
				}
				Log.d(TAG, "---close cursor + " + c);
				c.close();
				c = null;
			}
		}
	}
	
	private void appendOKItem(String food) {
		appendItem(food, FoodsColumns.PRIORITY_0, FoodsColumns.DETAIL_OK);
	}
	
	private void appendOKItem(String food1, String food2) {
		appendItem(food1, food2, FoodsColumns.PRIORITY_0, FoodsColumns.DETAIL_OK);
	}
	
	private void appendItem(String food, String priority, String detail) {
		Log.i(TAG, "UNSUITABLE: " + food + ", " + priority + ", " + detail);
		
		ContentValues item = new ContentValues();
		item.put(FoodsColumns.FOOD, food);
		item.put(FoodsColumns.PRIORITY, priority);
		item.put(FoodsColumns.DETAIL, detail);
		mDigestList.add(item);
	}
	
	private void appendItem(String food1, String food2, String priority, String detail) {
		Log.i(TAG, "CONFLICT: " + food1 + ", " + food2 + ", " + priority + ", " + detail);
		
		ContentValues item = new ContentValues();
		item.put(FoodsColumns.FOOD, food1 + " + " + food2);
		item.put(FoodsColumns.PRIORITY, priority);
		item.put(FoodsColumns.DETAIL, detail);
		this.mDigestList.add(item);
	}

	@Override
	public String[] getColumnNames() {
		return COLUMNS;
	}	

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return COLUMNS.length;
	}

	@Override
	public int getColumnIndex(String columnName) {
		if (columnName == null) {
			return -1;
		} else if (TextUtils.equals(columnName, COLUMNS[COLUMN_ID])) {
			return COLUMN_ID;
		} else if (TextUtils.equals(columnName, COLUMNS[COLUMN_FOOD])) {
			return COLUMN_FOOD;
		} else if (TextUtils.equals(columnName, COLUMNS[COLUMN_PRIORITY])) {
			return COLUMN_PRIORITY;
		} else if (TextUtils.equals(columnName, COLUMNS[COLUMN_DETAIL])) {
			return COLUMN_DETAIL;
		}
		
		return super.getColumnIndex(columnName);
	}

	@Override
	public String getColumnName(int columnIndex) {
		if (columnIndex < 0 || columnIndex >= COLUMNS.length) {
			return null;
		}
		
		return COLUMNS[columnIndex];
	}

	@Override
	public int getCount() {		
		return mDigestList.size();
	}

	@Override
	public double getDouble(int column) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public float getFloat(int column) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getInt(int column) {
		if (column == COLUMN_ID) {
			return mIndex;
		} else if (column == COLUMN_PRIORITY) {
			return R.drawable.ic_launcher;
		}
		
		return 0;
	}

	@Override
	public long getLong(int column) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public short getShort(int column) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getString(int column) {
		switch (column) {
		case COLUMN_FOOD:
		case COLUMN_PRIORITY:
		case COLUMN_DETAIL:
			return mDigestList.get(mIndex).getAsString(COLUMNS[column]);
		}
		return null;
	}

	@Override
	public boolean isNull(int column) {
		if (column < 0 || column >= COLUMNS.length) {
			return true;
		}
		
		if (mIndex < 0 || mIndex >= mDigestList.size()) {
			return true;
		}
		
		return mDigestList.get(mIndex).getAsString(COLUMNS[column]) == null;
	}

	@Override
	public boolean onMove(int oldPosition, int newPosition) {
		boolean rc = false;
		
		if (newPosition >= 0 && newPosition < mDigestList.size()) {
			mIndex = newPosition;
			rc = true;
		}
		
		return rc;
	}

	@Override
	public void close() {
		mDigestList.clear();
		
		mFDBHelper.close();
		
		super.close();
	}
}
